/**
 * 
 */
package objects;

import java.util.Iterator;
import java.util.Vector;

/**
 * @author Asaf
 * the forms
 */
public class Frm {
	private Vector <Trd> treads;
	private String _nameOfFourm;
	public Frm(String nameOfFourm)
	{
		this._nameOfFourm=nameOfFourm;
		treads =new Vector<Trd>();
	}
	public void addThread(Trd thread)
	{
		this.treads.add(thread);
	}
	/**
	 * getter
	 * @return the name of the forum
	 */
	public String getNameOfFourm() {
		return this._nameOfFourm;
	}
	public void set_nameOfForum(String name) {
		this._nameOfFourm = name;
	}
	
	public Trd addThread(String nameOfThread)
	{
		Trd t=new Trd(nameOfThread);
		this.treads.add(t);
		return t;
		
	}
	private Trd findThread(String nameOfThread)
	{
		for (Iterator iterator = this.treads.iterator(); iterator.hasNext();) {
			Trd t = (Trd) iterator.next();
			if (t.get_nameOfThread().equals(nameOfThread))
			{
				return t;
			}
			
		}
		return null;
	}
	/**
	 * 
	 * @param nameOfThread the name of the thread we want to add the mssage
	 * @param msg the message itself 
	 */
	public void addMessageToThread (String nameOfThread,User u,String msg)
	{
		
		try {
			findThread(nameOfThread).addMessage(u,msg);
		} catch (NullPointerException e) {
			System.out.println("can't find thread called:" + nameOfThread );
		}
		
	}
	/**
	 * 
	 * @param nameOfThread
	 * @param msg
	 * @return -2 if thread doesn't found , return -1 if message doesn't found ,/
	 * return 1 if object remove
	 */
	public int removeMessageFromThread (String nameOfThread,String msg)
	{

		Trd t=findThread(nameOfThread);
		if (t==null)
			return -2;
		return t.removeMessage(msg);
		
	}
	/**
	 * 
	 * @param nameOfThread
	 * @param TitleOfMessage
	 * @param NewContectOfMessage
	 * @return -2 if thread doesn't found , return -1 if msg doesn't found ,
	 *  -6 if user not manager or owner,return 1 on seccss 
	 */
	public int editMessageInThread(User u,String nameOfThread , String titleOfMessage , String newContectOfMessage)
	{
		Trd t=findThread(nameOfThread);
		if (t==null)
			return -2;
		Vector <Message> m=t.getMessages();
		for (Iterator iterator = m.iterator(); iterator.hasNext();)
		{
			Message message = (Message) iterator.next();
			if (message.getTitleOfMessage().trim().equals(titleOfMessage.trim()))
			{
				if (message.getOwner().equals(u)||u.getTypeOfUser().equals(User.Type.MANAGER))
						{
							message.setMessage(newContectOfMessage);
							return 1;
						}
				else return -6;
			}
			
		}
		return -1;
	}
	public Vector <Message> getMessageFromThread (String nameOfThread)
	{
		try{
		return findThread(nameOfThread).getMessages();
		} catch (NullPointerException e) {
			System.out.println("can't find thread called:" + nameOfThread );
		}
		return null;
	}
	/**
	 * @return the threads
	 */
	public Vector<Trd> getThreads() {
		return this.treads;
	}
	@Override
	public String toString()
	{
		return _nameOfFourm;
	}
	
	


}
